/**
 * 
 */
package com.bhugo.tests;

import static org.junit.Assert.*;

import java.util.ArrayList;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.bhugo.Driver;
import com.bhugo.IPStore;

/**
 * @author benjamin
 *
 */
public class DriverTest {

	/**
	 * @throws java.lang.Exception
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void splitTest() {
		ArrayList<String> arr = Driver.splitIPAndInstances("     192. ...167 . 3   . 50: ::: 9,,8    7, 6        ");
		String result = "";
		for (int i = 0; i < arr.size(); ++i)
			result += arr.get(i) + ',';
		assertEquals("Splitting \"     192. ...167 . 3   . 50: ::: 9,,8    7, 6        \" should give the list [192,167,3,50,9,8,7,6]","192,167,3,50,9,8,7,6,",result);
	}
	@Test
	public void simpleMergeTest() {
		Driver.instances.clear();
		Driver.addInstances("127.0.0.1:5,9,7,6");
		Driver.addInstances("127.0.0.1:1,2,9,5");
		Driver.addInstances("127.0.0.3:40,60");
		Driver.addInstances("125.192.12.1:");
		assertTrue("There should be 3 instances on the map",Driver.instances.size() == 3);
		
		String[] addr = {"127","0","0","1"};
		ArrayList<Integer> arr = Driver.instances.get(new IPStore(addr));
		String result = "";
		for (int i = 0; i < arr.size(); ++i){
			result += arr.get(i) + ",";
		}
		assertEquals("Merging 127.0.0.1:5,9,7,6 and 127.0.0.1:1,2,9,5 should result in 127.0.0.1:5,9,7,6,1,2,9,5","5,9,7,6,1,2,9,5,",result);
		
		String [] addr1 = {"127","0","0","3"};
		arr = Driver.instances.get(new IPStore(addr1));
		result = "";
		for (int i = 0; i < arr.size(); ++i){
			result += arr.get(i) + ",";
		}
		assertEquals("Adding 127.0.0.3:40,60 should have result 127.0.0.3:40,60","40,60,",result);
		
		String [] addr2 = {"125","192","12","1"};
		arr = Driver.instances.get(new IPStore(addr2));
		assertTrue("Adding 125.192.12.1: should have no elements in its list",arr.size() == 0);
		
		Driver.removeDuplicates();
		arr = Driver.instances.get(new IPStore(addr));
		result = "";
		for (int i = 0; i < arr.size(); ++i){
			result += arr.get(i) + ",";
		}
		assertEquals("Removing duplicates from 127.0.0.1:5,9,7,6 and 127.0.0.1:1,2,9,5 should result in 127.0.0.1:5,9,7,6,1,2","1,2,5,6,7,9,",result);
	}
	@Test
	public void malformedIPTest() {
		Driver.reset();
		assertTrue("Blank line is valid input",Driver.checkCompatibleFormat(" 	"));
		assertTrue("Valid input",Driver.checkCompatibleFormat("127.168.0.20"));
		assertTrue("Valid input",Driver.checkCompatibleFormat("127.168.0.20:"));
		assertTrue("Valid input",Driver.checkCompatibleFormat("127.168.0.20:1"));
		assertTrue("Valid input",Driver.checkCompatibleFormat("127.168.0.20 1,3,1,56,123123"));		
		assertTrue("Valid input",Driver.checkCompatibleFormat(" 127.168 .. 0 .20.:1,3 , 1,,56, 123123"));
		assertFalse("Invalid ip",Driver.checkCompatibleFormat("127.168.0"));
		assertFalse("Invalid ip",Driver.checkCompatibleFormat(" 127.168 .. 0 .::1,3  1,,56, 123123"));
		assertFalse("Invalid ip",Driver.checkCompatibleFormat(" .168 .. 0 .20::1,3 , 1,,56, 123123"));
	}
	
}
